package com.ufox.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ufox.entity.*;
import com.ufox.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 供应商信息表 前端控制器
 * </p>
 *
 * @author 优狐科技-www.hbufox.com
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;


    @GetMapping("/{id}")
    protected Result selectById(@PathVariable Integer id) {
        Supplier supplier = supplierService.getById(id);
        return Result.ok(supplier);
    }

    //所有供应商 动态获取
    @GetMapping
    public Result findAllCategory() {
        List<Supplier> list = supplierService.list();
        return Result.ok(list);
    }

    //分页 查询
    @PostMapping("/{page}/{size}")
    public Result findAllCond(@PathVariable int page, @PathVariable int size,@RequestBody SupplierCond supplierCond) {
        Page<Supplier> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<Supplier> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(supplierCond.getName()), Supplier::getName, supplierCond.getName());
        wrapper.like(StringUtils.isNotBlank(supplierCond.getLinkman()),Supplier::getLinkman, supplierCond.getLinkman());
        wrapper.like(StringUtils.isNotBlank(supplierCond.getMobile()), Supplier::getMobile, supplierCond.getMobile());
        Page<Supplier> list = supplierService.page(pageInfo, wrapper);
        return Result.ok(list);
    }

    //添加  或 修改
    @PostMapping
    public Result saveOrUpdateSupplier(@RequestBody Supplier supplier) {
        boolean b = supplierService.saveOrUpdate(supplier);
        return  (b ? Result.ok() : Result.error(""));
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delSupplier(@PathVariable Integer id) {
        boolean b = supplierService.removeById(id);
        return  (b ? Result.ok() : Result.error(""));
    }

    //批量删除
    @PostMapping("/batch")
    public Result delSuppliersBatch(@RequestBody List<Integer> ids) {
        boolean b = supplierService.removeByIds(ids);
        return  (b ? Result.ok() : Result.error(""));
    }


}

